package color.koitq.picker.ad;

import android.os.Bundle;

import androidx.annotation.Nullable;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import color.koitq.picker.base.BaseFragment;


public abstract class AdFragment extends BaseFragment {


    protected void fragmentAdClose() {
    }

    private boolean isShowDialog = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    protected void showVideoAd() {
        if (AdConfig.adDisable) {
            fragmentAdClose();
            return;
        }
        AdActivity.curVideoClass = getClass().getName();
        if (AdConfig.vacation == AdConfig.VACATION_HOLIDAY) {
            AdManager.getInstance().setActivity(requireActivity()).showVideoAdOnce();
        } else {
            fragmentAdClose();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void adClose(ADCloseEvent event) {
        if (getClass().getName().equals(AdActivity.curVideoClass)) {
            AdActivity.curVideoClass = null;
            fragmentAdClose();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void adDialogClose(ADDialogCloseEvent event) {
        if (isShowDialog) {
            isShowDialog = false;
        }
    }

}
